# -*- coding: utf-8 -*-
'''
Created on 27.04.2009

@author: renat
'''

from process import process
from process import inf
from sets import Set

import pickle
import os

def r1(self):
    return self.node == 4
def s1 (self):
    return self.node == 0 

inf.recieved = r1
inf.sent = s1

len='250'
rates =   [ '1','1.1','1.15','1.2','1.25','1.3','1.35','1.4','1.45','1.5','1.6','1.7']
#rates =   [ '1','1.1','1.15','1.2','1.25','1.3','1.35','1.4','1.45','1.5','1.6','1.7','1.8','2']
filKeys = Set([ '0 SNT AGT','0 SNT MAC', '0 n1:IFQ','1 COL', '2 COL','3 COL', '4 DUP', '4 RCV AGT'])

cts = ''
#len = '180'
#rates =   [ '1','1.1','1.15','1.2','1.25','1.3','1.35','1.4','1.45','1.5','1.6']
#filKeys = None
#rates =  ['2','2.5','3','3.5','4']
#,'1.7','1.8','1.9','2']

resr = {}
allkeys =Set()

descr = {'0 SNT AGT':'Переданно с 1-го узла',
         '0 SNT MAC':'Переданно с 1-го узла с учетом перепосылок', 
         '0 n1:IFQ':'Потери из очереди 1-го узла',
         '1 COL':'Интенсивность коллизий на 2-м узле', 
         '2 COL':'Интенсивность коллизий на 3-м узле',
         '3 COL':'Интенсивность коллизий на 4-м узле', 
         '4 DUP':'Дублирование пакетов на 5-м узле', 
         '4 RCV AGT':'Принято на 5-м узле'}

os.system("rm ../data/[0-9]+")
os.system("rm ../data/overall")

for rate in rates:
    print rate, "start"
    if not os.path.exists("../raw/strait."+rate+"Mb."+len+"."+cts):
        os.system("cd .. ; ~/ns-allinone-2.33/ns/ns ./1.tcl "+rate+"Mb "+len+ " "+cts)
    if os.path.exists("../data/st_rate"+rate+"."+len+cts):
        f = open("../data/st_rate"+rate+"."+len+cts, "r")
        resr[rate] = pickle.load(f)
        f.close()
        print "rate loaded"
    else:
        f = open("../raw/strait."+rate+"Mb."+len+"."+cts,"r")
        proc = process(5)
        proc.mode = "a"
        proc.dct = 20
        proc.gkey = rate
        proc.open()
        proc.parse(f);
        proc.close();
        resr[rate] = proc.pack_stats
        fpi = open("../data/st_rate"+rate+"."+len+cts, "w")
        pickle.dump(proc.pack_stats,fpi)
        fpi.close()
        print "rate generated"
    allkeys =  allkeys | Set(resr[rate].iterkeys())
    print rate, "done"
    

allk2 = list(allkeys)
allk2.sort()   

drf = open("../data/str"+len+cts+".csv",'w')
drf.write("#\t");
for key1 in allk2:
    drf.write(key1+"\t")
drf.write("\n");
for rate in rates:
    map = resr[rate]
    drf.write(rate+"\t")
    for key in allk2: 
        if key in map: 
            drf.write(`map[key]*8.0/15000.0`+"\t")
        else:
            drf.write("0\t")
    drf.write("\n")
drf.close()

splot = None
i =2 


for key in allk2:
    if not filKeys or key in filKeys:
        if key in descr:
            d = descr[key]
        else:
            d=key
        if splot:
            splot += ",'../data/str"+len+cts+".csv' using 1:"+`2+allk2.index(key)`+" title '"+d+"' "
        else:
            splot = "'../data/str"+len+cts+".csv' using 1:"+`2+allk2.index(key)`+" title '"+d+"' "
        i+=1

if False:
    psc = open("a-s.plot","w")
    #psc = os.popen("gnuplot", "w")
    
    psc.write("set terminal png size 900,800 font '/usr/share/fonts/truetype/FreeSans.ttf' 12\n"+ 
    "set encoding utf8 \n"+
    "set xlabel 'Мб/c' \n"+
    "set ylabel 'Мб/c' \n"+
    "set key outside center bottom \n"+
    "set output '../graphs/str"+len+cts+".png'\n" +  
    "plot " + splot + "\n")
    psc.close()